package co.dreamapps.dodoattack.activities;



import co.dreamapps.dodoattack.R;
import co.dreamapps.dodoattack.configuration.Configuration;
import co.dreamapps.dodoattack.configuration.Constants;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

/**
 * An example full-screen activity that shows and hides the system UI (i.e.
 * status bar and navigation/system bar) with user interaction.
 * 
 * @see SystemUiHider
 */
public class MenuActivity extends Activity implements OnClickListener {

	/**
	 * Configuration
	 */
	private Configuration config;

	/**
	 * Visual elements
	 */
	private EditText changeNameEditText;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// Oppening configuration
		config = new Configuration(this);

		// Configure window
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_menu);
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

		// Buttons
		ImageButton findChickensInFarmButton = (ImageButton) findViewById(R.id.findChickensInFarmButton);
		findChickensInFarmButton.setOnClickListener(this);
		ImageButton createFarmButton = (ImageButton) findViewById(R.id.createFarmButton);
		createFarmButton.setOnClickListener(this);

		// EditText
		changeNameEditText = (EditText) findViewById(R.id.changeNameEditText);
		changeNameEditText.setOnClickListener(this);
		changeNameEditText.setText(config.username);
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {

		case R.id.createFarmButton:

			if(config.username.length() == 0) {
				changeName();
			} else {
				startGame();
			}
			break;

		case R.id.findChickensInFarmButton:
			if(config.username.length() == 0) {
				changeName();
			} else {
				Intent intent = new Intent(this, MenuScrollerActivity.class);
				intent.putExtra(Constants.MODE, Constants.MODE_ATTACKING);
				startActivity(intent);
			}
			break;

		case R.id.changeNameEditText:
			changeName();
			break;
		}
	}

	public void changeName() {
		AlertDialog.Builder alert = new AlertDialog.Builder(this);
		alert.setTitle("Your name");
		alert.setMessage("Write someting that identifies you in the farm");
		final EditText input = new EditText(this);
		input.setText(changeNameEditText.getText());
		alert.setView(input);
		alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
				changeNameEditText.setText(input.getText());
				config.username = input.getText().toString();
			}
		});
		alert.show();
	}

	public void startGame() {
		AlertDialog.Builder alert = new AlertDialog.Builder(this);
		alert.setTitle("Starting a new game");
		alert.setMessage("Set a password");
		final EditText input = new EditText(this);
		input.setText(changeNameEditText.getText());
		alert.setView(input);
		alert.setPositiveButton("Start", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
				if(input.length() < 3) {
					Toast.makeText(getApplicationContext(), "Password must have at least 3 characters", Toast.LENGTH_SHORT).show();
				} else {
					Intent intent = new Intent(getApplicationContext(), MenuScrollerActivity.class);
					intent.putExtra(Constants.MODE, Constants.MODE_RACING);
					intent.putExtra(Constants.PASSWORD, input.getText().toString());
					startActivity(intent);
				}
			}
		});
		alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
				// Do nothing
			}
		});
		alert.show();
	}

	@Override
	protected void onPause() {
		config.commitChanges();
		super.onPause();
	}

}
